package com.game.sdk;

import java.util.ArrayList;
import java.util.List;

import android.text.TextUtils;
import org.json.JSONObject;

import com.game.sdk.domain.ChannelMessage;
import com.game.sdk.domain.DesDeclaration;
import com.game.sdk.domain.DeviceMsg;
import com.game.sdk.domain.Proclamation;
import com.game.sdk.domain.ResultCode;
import com.game.sdk.domain.UserInfo;
import com.game.sdk.util.GetDataImpl;
import com.game.sdk.util.Logger;
import com.game.sdk.util.ThreadPoolManager;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

/**
 * author janecer 2014年7月22日上午9:46:00 sdk系统核心类
 */
public class YTAppService extends Service {

	public static UserInfo userinfo;
	public static String gameid;
	public static String gameWeb;
	public static String qqNum;
	public static String qqKey;
	public static String appid;
	public static String agentid;
	public static String service_tel;
	public static String service_qq;
	public static String service_wx;
	public static String ptbkey;
	public static int ttbrate = 1;
	public static String notice;
	public static int ttb;
	public static int yxb;
	public static DeviceMsg dm;// 设备信息
	public static List<ChannelMessage> channels;
	public static List<ChannelMessage> channelsAll;
	public static Proclamation proclation;
	public static String clientIdEntifier;
	public static String clientId;
	public static String clientKey;
	public static String version = "1.0";
	public static int isPortrait = 1;
	public static int loginFlag = 1;

	public static double selecMoney =2.0;
	public static ChannelMessage[] channelsArray = new ChannelMessage[] {
			new ChannelMessage(1, "平台支付", "平台支付", "ptbpay"),
			new ChannelMessage(1, "游戏币支付", "游戏币支付", "gamepay"),
			new ChannelMessage(1, "支付宝支付", "支付宝支付", "alipay"),
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "nowpay"),
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "payeco"),// 微信支付（wftPay）
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "yeepay"),// 易宝支付（yib）
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "shengpay"),
			new ChannelMessage(1, "微信支付", "微信支付", "spay"),// 威富通
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "heepay"), //
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "tclpay"),//
			new ChannelMessage(1, "财付通支付", "财付通支付", "tenpay"),//
			new ChannelMessage(1, "微信通支付", "微信支付", "wxpay"),//
			new ChannelMessage(1, "百度钱包支付", "百度钱包支付", "baidupay"), //
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "hrpay"), // 华融
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "hrpay"),// 华融
			new ChannelMessage(1, "银行卡支付", "银行卡支付", "hrpay") // 华融
	};

	public static boolean isLogin = false;// 判断用户是否登录
	public static int isgift = 1;// 判断用户是否登录
	private static Context actx;

	public static byte[] keyValue;
	public static byte[] iv;
	public static char[] k;

	static DesDeclaration desDeclaration;
	public static int checked;
	public static String ip;
	public static int is_update;
	public static String url;
	public static String service_qqGroup;
	public static String city_id = "";
	public static boolean inited;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	public static void startService(Context ctx) {
		if (!isServiceRunning(ctx)) {
			actx = ctx;
			Intent intent_service = new Intent(ctx, YTAppService.class);
			intent_service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			ctx.startService(intent_service);
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		String succ = "";
		try {
			succ = intent.getStringExtra("login_success");
		} catch (Exception e) {
			succ = "";
			// e.printStackTrace();
		}

		initPayChannels();
		handCommand();
		return START_STICKY;
	}

	/**
	 * @author lingguihua
	 * @原因 每次该支付的时候都需要去该重新些数据，我这里就直接些就可以了
	 * */
	private void initPayChannels() {
		if (null == channelsAll) {// 初始化支付渠道
			channelsAll = new ArrayList<ChannelMessage>();
			if (channelsArray.length > 0) {// 不止一个item
				for (int i = 0; i < channelsArray.length; i++) {
					channelsArray[i].channelId = i + 1;
					channelsAll.add(channelsArray[i]);
				}
			}
		}
	}

	private void handCommand() {
//		Notification notification = new Notification(0, "",
//				System.currentTimeMillis());
//		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
//				new Intent(), 0);
//		notification.setLatestEventInfo(this, "", "", contentIntent);
//		startForeground(0, notification);
		  PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
	                new Intent(), 0);

	      Notification.Builder builder = new Notification.Builder(this)
	                .setAutoCancel(true)
	                .setContentIntent(contentIntent)
	                .setWhen(System.currentTimeMillis())
	                .setOngoing(true);
	      Notification notification = builder.getNotification();
	        startForeground(0, notification);
	}

	/**
	 * 用来判断当前服务类是否在运行
	 * 
	 * @param ctx
	 * @return
	 */
	public static boolean isServiceRunning(Context ctx) {
		ActivityManager am = (ActivityManager) ctx
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> runServices = am
				.getRunningServices(Integer.MAX_VALUE);
		if (null == runServices || 0 == runServices.size()) {
			return false;
		}
		for (int i = 0; i < runServices.size(); i++) {
			if (runServices.get(i).service.getClassName().equals(
					YTAppService.class.getName())) {
				return true;
			}
		}
		return false;
	}
	/***
	 * 增加一个 通过moeny来过滤支付模块的过滤器
	 * */
	public static List<ChannelMessage> getPayChannel(double money,String type){
		if(!TextUtils.isEmpty(type)){
			if(money<selecMoney){
				List<ChannelMessage> list = new ArrayList<ChannelMessage>();
				list.addAll(channels);
				if(list.size()>0){
					for(int index=0;index<list.size();index++){
						ChannelMessage msg= list.get(index);
						if(type.equals(msg.type)){
							list.remove(msg);
							break;
						}
					}
				}
				return list;
			}
		}
		return channels;
	}

}
